//hint(ENABLE_STROKE_PURE)  // this code will make single line graphics nicer, but slower
// simulator FlowerState = 32768
/* remember some settings are for hardware specific needs, for the iMito
consider hardcoding the positioning of the middle of the screen, due to
the GUI bar at the bottom of the screen--I've seen it create an offset when
using displayHeight and displayWidth 

currently centering is accomplished with displayH/ dW so the application can run
nicely on android phones.
*/

boolean androidMode;
boolean monitorMode;
boolean facetAct;
boolean seqAct=false;
PImage seqImg;
PImage bImg;
PImage layerImg;
PGraphics seqBuffer;
PGraphics bigBuffer;
int clearScreen=0;
boolean alignCherish;
String statMsg="";
String flowStatMsg="";
int PID;

File bloomSrc = new File("/mnt/sdcard/sakura/bloom.i");

//stage control
int knownFacet=-1;

void InitBloom() {
  String bloom[] = loadStrings(bloomSrc);
  bloomName = bloom[1];
  bloomI = int(bloom[0]);
  println("S:sakura? "+bloom[1]);
  if (bloomI!=15||bloom[1].equals("Sakura")==true) {
    androidMode=true;
    println("S:Androidmode= "+androidMode);
  } else {
    monitorMode=true;
  }
  /* format
   8 [flower index]
   I [flower name]
   0.0 gravity
   0.0 sakura shape
   0.0 arbitrary
   */
}
/*
void AndroidOnly() {
  PID = android.os.Process.myPid();
  println(PID);
}
*/
void setup() {
//  size(displayWidth,displayHeight,JAVA2D);
  orientation(LANDSCAPE);
  size(960, 720, JAVA2D);  //1280 or 960??
  background(0);
  //prefered modes, might need only once?
  imageMode(CENTER); 
  colorMode(HSB, 255);
  textFont(createFont("sans-serif", 27));
  seqBuffer = createGraphics(360, 360, JAVA2D);
  bigBuffer = createGraphics(960, 720, JAVA2D);
  InitBloom();
  commonSetup(); //for chymera and sakura
  alignCherish=true;
  layerImg = loadImage("/mnt/sdcard/sakura/utility/align1.png");
  DoAlign();
}
void PutImage (PImage img) {
  if (androidMode) {

//    image(bImg, 480, 360, 960, 720);
    image(bImg, displayWidth/2, displayHeight/2, displayHeight*4/3, displayHeight);
  } else {
    image(bImg, width/2, height/2, width, height);
  }
}

void BuffImage (PGraphics bigBuffer, PImage pic) {
  bigBuffer.beginDraw();
  bigBuffer.imageMode(CENTER);
  bigBuffer.background(0);
  bigBuffer.translate(bigBuffer.width/2, bigBuffer.height/2);
  bigBuffer.rotate(radians(0)); //f_imgseq.rotFO));
  bigBuffer.image(pic, 0, 0, 960, 720);
  bigBuffer.endDraw();
}
void draw() {
  if (callSeqStart) {
    StartSeq(updateSeq);
  }
  if (facetAct && seqAct) {
    DrawSeq(seqBuffer);
    bImg = seqBuffer.get(0, 0, 360, 360);
    PutImage(bImg);
  }
  if (clearScreen>0) {
    fill(0, 0, 0, 255/clearScreen);
    if (androidMode) {
      rect(0, 0, 1280, 720);
    } else {
      rect(0, 0, width, height);
    }
    clearScreen--;
  }
  if (alignCherish) {
    DoAlign();
  }
  if (!androidMode) {
    DisplayHeadsUp();
  }
}

void DoAlign() {
  int toggle=millis()/9000%9;
  BlankScreen();
  /*
  if (androidMode) {
    if (toggle%2>1) {
      blend(layerImg, 0, 0, 1280, 720, -160, 0, 1280, 720, EXCLUSION);
    }
  } else {
  }
  if (toggle%2==1) {
    blend(layerImg, 0, 0, 1280, 720, -(width*160/960), 0, width*4/3, height, DARKEST);
  } else {
    blend(layerImg, 0, 0, 1280, 720, -(width*160/960), 0, width*4/3, height, EXCLUSION);
  }
  */
}

void BlankScreen() {
  //do not use background image, android can't use resize code to match
  //java vs android screen display switch methods needed for 
  //cherish control and display. stick with regular layering
  bImg = loadImage("/mnt/sdcard/sakura/b/blank.png");
  if (androidMode) {
//    image(bImg, 480, 360, 1280, 720);
    image(bImg, displayWidth/2,displayHeight/2,displayHeight*4/3,displayHeight);
  } else {
    image(bImg, width/2, height/2, width, height);
  }
}

void keyPressed() {
  println(key);
  println("S:seqAct :"+seqAct);
  if (key=='/') {
    println("S:testing procedures");
    runTest();
  }
}

void StartSeq(boolean update) {
  F_Seq f_imgseq = (F_Seq) SeqList.get(pTypeID);
  f_imgseq.setupFO(update, launchSeed);
  callSeqStart=false;
}  

void DrawSeq(PGraphics seqBuffer) {
  F_Seq f_imgseq = (F_Seq) SeqList.get(actSeqID);
  seqImg=f_imgseq.drawFO();
  seqBuffer.beginDraw();
  seqBuffer.imageMode(CENTER);
  seqBuffer.background(0);
  seqBuffer.translate(seqBuffer.width/2, seqBuffer.height/2);
  seqBuffer.rotate(radians(f_imgseq.rotFO));
  seqBuffer.image(seqImg, 0, 0, 360, 360);
  seqBuffer.endDraw();
}
/* for android phone demo
public void onPause() { 
  System.exit(0);
  super.onPause();
}
*/
void runTest() {
  println("S:runtest");
}
